﻿Imports System.IO
Imports System.IO.File
Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.PMAlign
Imports Cognex.VisionPro.CalibFix
Imports Cognex.VisionPro
Imports System.Math
Public Class FrmAddProd2
    Dim TextTorq(30) As System.Windows.Forms.TextBox
    Dim TextAngle(30) As System.Windows.Forms.TextBox
    Dim TextFeeder(30) As System.Windows.Forms.TextBox
    Dim f() As String
    Dim ToolPath As String = ""
    Dim pointNO As Integer = 0
    Dim CADPoint(32) As MITSUBISHI_Point
    Dim CADPointName(32) As String
    Dim flag(4) As Boolean

    Private Sub FrmAddProd_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        AddProdflag = False
    End Sub

    Private Sub FrmAddProd_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        InitControl()
        ToolPath = DebugPath & "\tool"
        GetDirectFiles(ToolPath)
        For I = 0 To 31
            CADPoint(I).X = 0
            CADPoint(I).Y = 0
            CADPoint(I).Z = 0
            CADPoint(I).C = 0
        Next
        CADPointName(0) = "photo1cad"
        Panel4.Visible = False
        AddProdflag = True

        Button_PrePoint.Enabled = False
        Button_SaveAll.Enabled = False
        For I = 0 To 4
            flag(4) = False
        Next
        TabControl1.SelectedIndex = 0

        ComboBox_OldProd.Focus()
        StepOK1 = False
        StepOK2 = False
        StepOK3 = False
        Try
            '启动机器人示教
            Dim robotpath As String = DebugPath & "\RobotConnect"
            '写地址
            File.WriteAllText(robotpath & "\pointPath.txt", DebugPath & "\Tool\" & TextBox_NewProdName.Text)
            '开启机器人软件
            Dim a As Integer
            a = Shell(robotpath & "\RT.exe", vbNormalFocus)
            If a = 0 Then
                MsgBox("机器人程序打开失败！！")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try


    End Sub

    Private Sub GetDirectFiles(ByVal s As String)
        '找出位于 s 根目录下的所有文件
        Try
            Dim i As Integer
            Dim t As Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(s), k As String, name() As String

            For Each k In My.Computer.FileSystem.GetDirectories(s)
                ReDim Preserve f(i)
                f(i) = k
                Dim myFileInfo As New IO.FileInfo(f(i))
                name = Split(f(i), "\")
                ComboBox_OldProd.Items.Add(name(UBound(name))) '获取文件名
                i += 1
            Next
        Catch
        End Try
    End Sub

    Private Sub Button_PreStep_Click(sender As Object, e As EventArgs) Handles Button_PreStep.Click

        If TabControl1.SelectedIndex > 0 Then
            TabControl1.SelectedIndex = TabControl1.SelectedIndex - 1
            If TabControl1.SelectedIndex = 0 Then
                Button_PreStep.Enabled = False
                Button_NextStep.Enabled = True
            Else
                Button_PreStep.Enabled = True
            End If
        End If
        Select Case TabControl1.SelectedIndex
            Case 0  '保存产品信息
                Label_ProdInfo.BackColor = Color.Yellow  'step1
                Label_ModelSet.BackColor = Color.WhiteSmoke 'step2

            Case 1   '做模板
                Label_ModelSet.BackColor = Color.Yellow 'step2
                Label_PointWrite.BackColor = Color.WhiteSmoke 'step3
            Case 2  '示教点位
                Label_PointWrite.BackColor = Color.Yellow 'step3
                Label_PointPara.BackColor = Color.WhiteSmoke 'step4
            Case 3 '保存螺丝点信息
                Label_PointPara.BackColor = Color.Yellow 'step4
                Label_Complete.BackColor = Color.WhiteSmoke 'step5
        End Select

    End Sub

    Private Sub Button_NextStep_Click(sender As Object, e As EventArgs) Handles Button_NextStep.Click

        SaveData(TabControl1.SelectedIndex)
    End Sub

    Private Sub SaveData(ByVal page As Integer)
        Select Case page
            Case 0  '保存产品信息
                Label_ProdInfo.BackColor = Color.Lime  'step1
                Label_ModelSet.BackColor = Color.Yellow 'step2
                '复制产品，并获取检测工具数组
                If ComboBox_OldProd.Text = "" Then
                    MsgBox("相似产品未指定！")
                    GoTo d
                End If
                If TextBox_NewProdName.Text = "" Then
                    MsgBox("新产品名未指定！")
                    GoTo d
                End If
                If TextBox_PointCount.Text = "" Then
                    MsgBox("锁附螺丝颗数未指定！")
                    GoTo d
                End If
                If ComboBox_OldProd.Text = TextBox_NewProdName.Text Then
                    Select Case MsgBox("已存该在产品，是否修改产品？", MsgBoxStyle.YesNo)
                        Case MsgBoxResult.Yes
                            '确定要执行的
                        Case MsgBoxResult.No
                            '取消要执行的
                            GoTo d
                    End Select
                End If
                Try
                    If ComboBox_OldProd.Text <> TextBox_NewProdName.Text Then
                        Dim name As String = TextBox_NewProdName.Text
                        Dim path As String = ToolPath & "\" & ComboBox_OldProd.Text '& "\"
                        If Dir(path, FileAttribute.Directory) <> "" Then
                            My.Computer.FileSystem.CopyDirectory(path, ToolPath & "\" & name)
                        End If
                    End If

                    '保存螺丝颗数和螺丝型号
                    Dim filepath = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\"
                    Dim Para_num As Integer = FreeFile()
                    'Dim OpenParameter As Boolean = Exists(filepath & "Parameter.txt")
                    Dim PointCount As Integer = Val(TextBox_PointCount.Text)
                    Dim ScrewSN As String = TextBox_ScrewSN.Text
                    Dim NEWScrewPara As ScrewPara
                    NEWScrewPara.PointCount = PointCount
                    NEWScrewPara.ScrewSN = ScrewSN
                    FileOpen(Para_num, filepath & "Parameter.txt", OpenMode.Random)
                    FilePut(Para_num, NEWScrewPara)
                    FileClose(Para_num)
                    flag(1) = True
                    FrmMain.TCmbPro.Text = TextBox_NewProdName.Text
                    If flag(2) = False Then
                        Button_NextStep.Enabled = False
                    Else
                        Button_NextStep.Enabled = True
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                '发送数据库地址至机器人
                Try
                    Dim robotpath As String = DebugPath & "\RobotConnect"
                    '写地址
                    File.WriteAllText(robotpath & "\pointPath.txt", DebugPath & "\Tool\" & TextBox_NewProdName.Text)
                    'Dim result As Integer = SendMsgToRobot("CADPath")
                Catch ex As Exception
                    MsgBox(ex.Message.ToString)
                End Try

            Case 1   '做模板

                Label_ModelSet.BackColor = Color.Lime 'step2
                Label_PointWrite.BackColor = Color.Yellow 'step3
                If flag(3) = False Then
                    Button_NextStep.Enabled = False
                Else
                    Button_NextStep.Enabled = True
                End If

            Case 2  '示教点位

                Label_PointWrite.BackColor = Color.Lime 'step3
                Label_PointPara.BackColor = Color.Yellow 'step4
                pointNO = 0
                CADPointName(pointNO) = "photo1cad"
                TextBox_pointName.Text = CADPointName(pointNO)
                If flag(4) = False Then
                    Button_SaveAll.Enabled = False
                Else
                    Button_SaveAll.Enabled = True
                End If

            Case 3 '保存螺丝点信息

                Label_PointPara.BackColor = Color.Lime 'step4
                Label_Complete.BackColor = Color.Yellow 'step5
                SavePointPara()
                If flag(4) = False Then
                    Button_SaveAll.Enabled = False
                Else
                    Button_SaveAll.Enabled = True
                End If
                Button_NextStep.Enabled = False
        End Select
        If TabControl1.SelectedIndex < TabControl1.TabCount Then
            TabControl1.SelectedIndex = TabControl1.SelectedIndex + 1
            Button_PreStep.Enabled = True
        End If
d:
    End Sub
    Private Sub SavePointPara()
        Dim i As Integer = 0
        Try
            Dim sql1 As String = ""
            Dim DBpath As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\PointPara.accdb"
            If File.Exists(DBpath) = False Then
                MsgBox("文件不存在：" & DBpath)
                GoTo d
            End If
            Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DBpath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
            For i = 1 To 30
                If i < 10 Then
                    sql1 = " update " & " PointPara " & "set torque=" & TextTorq(i).Text & ",angle=" & TextAngle(i).Text & ",feeder=" & TextFeeder(i).Text & ",disable=0" & " where PointName=" & "'set0" & i & "'"
                Else
                    sql1 = " update " & " PointPara " & "set torque=" & TextTorq(i).Text & ",angle=" & TextAngle(i).Text & ",feeder=" & TextFeeder(i).Text & ",disable=0" & " where PointName=" & "'set" & i & "'"
                End If

                HandleDB(sql1, cnStr)
            Next
            flag(4) = True
            MsgBox("点位参数保存成功！")
d:
        Catch ex As Exception
            MsgBox("SavePointPara err:" & ex.Message.ToString)
        End Try
    End Sub

    Private Sub InitControl()
        Label_ProdInfo.BackColor = Color.Yellow 'step1
        Label_ModelSet.BackColor = Color.WhiteSmoke 'step2
        Label_PointWrite.BackColor = Color.WhiteSmoke 'step3
        Label_PointPara.BackColor = Color.WhiteSmoke 'step4
        Label_Complete.BackColor = Color.WhiteSmoke 'step5

        TextTorq(0) = TextBox_TorqAll
        TextAngle(0) = TextBox_AngleAll
        TextFeeder(0) = TextBox_FeederAll

        TextTorq(1) = TextBox_Torq1
        TextAngle(1) = TextBox_Angle1
        TextFeeder(1) = TextBox_feeder1

        TextTorq(2) = TextBox_Torq2
        TextAngle(2) = TextBox_Angle2
        TextFeeder(2) = TextBox_feeder2

        TextTorq(3) = TextBox_Torq3
        TextAngle(3) = TextBox_Angle3
        TextFeeder(3) = TextBox_feeder3

        TextTorq(4) = TextBox_Torq4
        TextAngle(4) = TextBox_Angle4
        TextFeeder(4) = TextBox_feeder4

        TextTorq(5) = TextBox_Torq5
        TextAngle(5) = TextBox_Angle5
        TextFeeder(5) = TextBox_feeder5

        TextTorq(6) = TextBox_Torq6
        TextAngle(6) = TextBox_Angle6
        TextFeeder(6) = TextBox_feeder6

        TextTorq(7) = TextBox_Torq7
        TextAngle(7) = TextBox_Angle7
        TextFeeder(7) = TextBox_feeder7

        TextTorq(8) = TextBox_Torq8
        TextAngle(8) = TextBox_Angle8
        TextFeeder(8) = TextBox_feeder8

        TextTorq(9) = TextBox_Torq9
        TextAngle(9) = TextBox_Angle9
        TextFeeder(9) = TextBox_feeder9

        TextTorq(10) = TextBox_Torq10
        TextAngle(10) = TextBox_Angle10
        TextFeeder(10) = TextBox_feeder10

        TextTorq(11) = TextBox_Torq11
        TextAngle(11) = TextBox_Angle11
        TextFeeder(11) = TextBox_feeder11

        TextTorq(12) = TextBox_Torq12
        TextAngle(12) = TextBox_Angle12
        TextFeeder(12) = TextBox_feeder12

        TextTorq(13) = TextBox_Torq13
        TextAngle(13) = TextBox_Angle13
        TextFeeder(13) = TextBox_feeder13

        TextTorq(14) = TextBox_Torq14
        TextAngle(14) = TextBox_Angle14
        TextFeeder(14) = TextBox_feeder14

        TextTorq(15) = TextBox_Torq15
        TextAngle(15) = TextBox_Angle15
        TextFeeder(15) = TextBox_feeder15

        TextTorq(16) = TextBox_Torq16
        TextAngle(16) = TextBox_Angle16
        TextFeeder(16) = TextBox_feeder16

        TextTorq(17) = TextBox_Torq17
        TextAngle(17) = TextBox_Angle17
        TextFeeder(17) = TextBox_feeder17

        TextTorq(18) = TextBox_Torq18
        TextAngle(18) = TextBox_Angle18
        TextFeeder(18) = TextBox_feeder18

        TextTorq(19) = TextBox_Torq19
        TextAngle(19) = TextBox_Angle19
        TextFeeder(19) = TextBox_feeder19

        TextTorq(20) = TextBox_Torq20
        TextAngle(20) = TextBox_Angle20
        TextFeeder(20) = TextBox_feeder20

        TextTorq(21) = TextBox_Torq21
        TextAngle(21) = TextBox_Angle21
        TextFeeder(21) = TextBox_feeder21

        TextTorq(22) = TextBox_Torq22
        TextAngle(22) = TextBox_Angle22
        TextFeeder(22) = TextBox_feeder22

        TextTorq(23) = TextBox_Torq23
        TextAngle(23) = TextBox_Angle23
        TextFeeder(23) = TextBox_feeder23

        TextTorq(24) = TextBox_Torq24
        TextAngle(24) = TextBox_Angle24
        TextFeeder(24) = TextBox_feeder24

        TextTorq(25) = TextBox_Torq25
        TextAngle(25) = TextBox_Angle25
        TextFeeder(25) = TextBox_feeder25

        TextTorq(26) = TextBox_Torq26
        TextAngle(26) = TextBox_Angle26
        TextFeeder(26) = TextBox_feeder26

        TextTorq(27) = TextBox_Torq27
        TextAngle(27) = TextBox_Angle27
        TextFeeder(27) = TextBox_feeder27

        TextTorq(28) = TextBox_Torq28
        TextAngle(28) = TextBox_Angle28
        TextFeeder(28) = TextBox_feeder28

        TextTorq(29) = TextBox_Torq29
        TextAngle(29) = TextBox_Angle29
        TextFeeder(29) = TextBox_feeder29

        TextTorq(30) = TextBox_Torq30
        TextAngle(30) = TextBox_Angle30
        TextFeeder(30) = TextBox_feeder30
        For i = 0 To 30
            TextTorq(i).Text = "0"
            TextAngle(i).Text = "0"
            TextFeeder(i).Text = "0"
        Next
        ComboBox_OldProd.Items.Clear()
        TextBox_status1.BackColor = Color.WhiteSmoke
        TextBox_status1.Text = "等待产品流入..."
        TextBox_status2.BackColor = Color.WhiteSmoke
        TextBox_status2.Text = ""
        ComboBox_FLG1.SelectedIndex = 0
    End Sub

    Private Sub Button_AllSet_Click(sender As Object, e As EventArgs) Handles Button_AllSet.Click
        Try
            For i = 1 To 30
                TextTorq(i).Text = TextBox_TorqAll.Text
                TextAngle(i).Text = TextBox_AngleAll.Text
                TextFeeder(i).Text = TextBox_FeederAll.Text

            Next
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_SaveAll_Click(sender As Object, e As EventArgs) Handles Button_SaveAll.Click
        If TabControl1.SelectedIndex <> 4 Then
            MsgBox("步骤未完成！")
        Else
            Label_PointPara.BackColor = Color.Lime  'step4
            Label_Complete.BackColor = Color.Lime 'step5
            MsgBox("产品保存成功！")
        End If

    End Sub

    Private Sub Button_Tool1_Click(sender As Object, e As EventArgs) Handles Button_Tool1.Click
        gCurrProName = TextBox_NewProdName.Text
        FrmSetting.Show()
    End Sub

    Private Sub Button_SaveTool1_Click(sender As Object, e As EventArgs) Handles Button_SaveTool1.Click
        Try
            Dim i As Integer
            Dim path As String = ToolPath & "\" & TextBox_NewProdName.Text & "\"
            If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                MkDir(path)
            End If
            For i = 0 To Cam_num - 1
                If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                    MkDir(path)
                End If
                CogSerializer.SaveObjectToFile(gTBCheck(i), path & "TBCheck" & (i + 1).ToString & ".vpp")
            Next
            flag(2) = True
            Button_NextStep.Enabled = True
            MsgBox("工具保存成功！")
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_Step3_Click(sender As Object, e As EventArgs) Handles Button_Step3.Click
        Dim result As Integer
        Vision_NG = ""
        If Button_Step3.BackColor = Color.Lime Then
            StepOK2 = False
            Button_Step3.BackColor = Color.WhiteSmoke
            Button_Step3.UseVisualStyleBackColor = True
            StepNO = "STEP0"

            TextBox_status2.Text = ""
            TextBox_status2.BackColor = Color.WhiteSmoke
        Else
            Button_Step3.BackColor = Color.Lime
            StepNO = "STEP2"
            result = SendMsgToRobot("MANUAL")
            If result = 0 Then
                Button_Step3.BackColor = Color.WhiteSmoke
                Button_Step3.UseVisualStyleBackColor = True
                StepNO = "STEP0"
            End If
        End If

    End Sub

    Private Sub Button_TeachCAD_Click(sender As Object, e As EventArgs) Handles Button_TeachCAD.Click

        If Button_TeachCAD.BackColor = Color.Lime Then
            Button_TeachCAD.BackColor = Color.WhiteSmoke
            Button_TeachCAD.UseVisualStyleBackColor = True
            Panel4.Visible = False
        Else
            Button_TeachCAD.BackColor = Color.Lime
            Panel4.Visible = True

            Button_TeachRB.BackColor = Color.WhiteSmoke
            Button_TeachRB.UseVisualStyleBackColor = True

        End If
    End Sub

    Private Sub Button_TeachRB_Click(sender As Object, e As EventArgs) Handles Button_TeachRB.Click
        'If Button_TeachRB.BackColor = Color.Lime Then
        '    Button_TeachRB.BackColor = Color.WhiteSmoke
        '    Button_TeachRB.UseVisualStyleBackColor = True
        'Else
        '    Button_TeachRB.BackColor = Color.Lime
        Panel4.Visible = False
        Button_TeachCAD.BackColor = Color.WhiteSmoke
        Button_TeachCAD.UseVisualStyleBackColor = True

        Try
            '写入两个拍照点坐标
            Select Case MsgBox("将当前拍照坐标写入数据库，是否修改？", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    '确定要执行的
                    Dim sql1 As String = ""
                    Dim DBpath As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\cad_point.accdb"
                    Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DBpath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
                    sql1 = " update " & " domino " & "set x=" & AutoAddPhotoPoint(1).X & ",y=" & AutoAddPhotoPoint(1).Y & ",z=" & AutoAddPhotoPoint(1).Z & ",c=" & AutoAddPhotoPoint(1).C & " where PointName=" & "'photo1cad'"
                    HandleDB(sql1, cnStr)
                    sql1 = " update " & " domino " & "set x=" & AutoAddPhotoPoint(2).X & ",y=" & AutoAddPhotoPoint(2).Y & ",z=" & AutoAddPhotoPoint(2).Z & ",c=" & AutoAddPhotoPoint(2).C & " where PointName=" & "'photo2cad'"
                    HandleDB(sql1, cnStr)
                Case MsgBoxResult.No
                    '取消要执行的

            End Select

            '启动机器人示教
            Dim robotpath As String = DebugPath & "\RobotConnect"
            '写地址
            File.WriteAllText(robotpath & "\pointPath.txt", DebugPath & "\Tool\" & TextBox_NewProdName.Text)
            '开启机器人软件
            Dim a As Integer
            a = Shell(robotpath & "\RT.exe", vbNormalFocus)
            If a = 0 Then
                MsgBox("机器人程序打开失败！！")
                GoTo d
            End If
            flag(3) = True
            Button_NextStep.Enabled = True
d:
        Catch ex As Exception
            MsgBox("OpenUploadProcess:" & ex.Message.ToString & ex.StackTrace.ToString)
        End Try
        '  End If


    End Sub

    Private Sub Button_NextPoint_Click(sender As Object, e As EventArgs) Handles Button_NextPoint.Click
        If pointNO < 31 Then
            pointNO = pointNO + 1
            Select Case pointNO
                Case 0
                    CADPointName(pointNO) = "photo1cad"
                    Exit Select
                Case 1
                    CADPointName(pointNO) = "photo2cad"
                    Button_PrePoint.Enabled = True
                    Exit Select
                Case Is <= 10
                    CADPointName(pointNO) = "set0" & (pointNO - 1)
                    Exit Select
                Case Is > 10
                    CADPointName(pointNO) = "set" & (pointNO - 1)
                    If pointNO = 31 Then
                        Button_NextPoint.Enabled = False
                    End If
                    Exit Select
            End Select


        End If
        TextBox_pointName.Text = CADPointName(pointNO)
        TextBox_CADX.Text = CADPoint(pointNO).X
        TextBox_CADY.Text = CADPoint(pointNO).Y
        TextBox_CADZ.Text = CADPoint(pointNO).Z
        TextBox_CADC.Text = CADPoint(pointNO).C
    End Sub

    Private Sub Button_PrePoint_Click(sender As Object, e As EventArgs) Handles Button_PrePoint.Click
        If pointNO > 0 Then
            pointNO = pointNO - 1
            Button_NextPoint.Enabled = True
            Select Case pointNO
                Case 0
                    CADPointName(pointNO) = "photo1cad"
                    Button_PrePoint.Enabled = False
                    Exit Select
                Case 1
                    CADPointName(pointNO) = "photo2cad"
                    Exit Select
                Case Is <= 10
                    CADPointName(pointNO) = "set0" & (pointNO - 1)
                    Exit Select
                Case Is > 10
                    CADPointName(pointNO) = "set" & (pointNO - 1)

                    Exit Select
            End Select
        Else
            CADPointName(pointNO) = "photo1cad"
        End If
        TextBox_pointName.Text = CADPointName(pointNO)
        TextBox_CADX.Text = CADPoint(pointNO).X
        TextBox_CADY.Text = CADPoint(pointNO).Y
        TextBox_CADZ.Text = CADPoint(pointNO).Z
        TextBox_CADC.Text = CADPoint(pointNO).C
    End Sub


    Private Sub TextBox_CADX_TextChanged(sender As Object, e As EventArgs) Handles TextBox_CADX.TextChanged
        CADPoint(pointNO).X = Val(TextBox_CADX.Text)
    End Sub

    Private Sub TextBox_CADY_TextChanged(sender As Object, e As EventArgs) Handles TextBox_CADY.TextChanged
        CADPoint(pointNO).Y = Val(TextBox_CADY.Text)
    End Sub

    Private Sub TextBox_CADZ_TextChanged(sender As Object, e As EventArgs) Handles TextBox_CADZ.TextChanged
        CADPoint(pointNO).Z = Val(TextBox_CADZ.Text)
    End Sub

    Private Sub TextBox_CADC_TextChanged(sender As Object, e As EventArgs) Handles TextBox_CADC.TextChanged
        CADPoint(pointNO).C = Val(TextBox_CADC.Text)
    End Sub

    Private Sub ComboBox_FLG1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_FLG1.SelectedIndexChanged
        If ComboBox_FLG1.SelectedItem.ToString = "R" Then
            CADPoint(pointNO).FLG1 = 4 '右手姿态
        Else
            CADPoint(pointNO).FLG1 = 0 '左手姿态
        End If

    End Sub

    Private Sub Button_PointWrite_Click(sender As Object, e As EventArgs) Handles Button_PointWrite.Click
        Try
            If TextBox_NewProdName.Text = "" Then
                MsgBox("新建的产品名未指定！")
                GoTo d
            End If
            Dim SavePath As String = ToolPath & "\" & TextBox_NewProdName.Text & "\cad_point.accdb"
            If File.Exists(SavePath) = True Then
                For i = 0 To 31
                    Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & SavePath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
                    'Dim sql1 As String = " update " & " domino " & "set x=" & CADPoint(i).X & ",y=" & CADPoint(i).Y & ",z=" & CADPoint(i).Z & ",c=" & CADPoint(i).C & ",FLG1=" & CADPoint(i).FLG1 & " where PointName=" & "'" & CADPointName(i) & "'"
                    Dim sql1 As String = " update " & " domino " & "set x=" & CADPoint(i).X & ",y=" & CADPoint(i).Y & ",z=" & CADPoint(i).Z & ",c=" & CADPoint(i).C & " where PointName=" & "'" & CADPointName(i) & "'"

                    HandleDB(sql1, cnStr)
                Next
                flag(3) = True
                Button_NextStep.Enabled = True
                MsgBox("点位写入完成！")
            Else
                MsgBox("路径不存在！请检查路径：" & SavePath)
            End If
d:
        Catch ex As Exception
            MsgBox("点位写入发生错误：" & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_Exist_Click(sender As Object, e As EventArgs) Handles Button_Exist.Click
        Me.Close()
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If StepOK1 = True Then
            TextBox_status1.Text = "产品已经到位"
            TextBox_status1.BackColor = Color.Lime
            TextBox_status2.Text = "准备匹配模板"
        End If
        If StepOK2 = True Then
            TextBox_status2.Text = "自动匹配模板中,请等待..."
            TextBox_status2.BackColor = Color.Yellow
        End If

        If StepOK3 = True Then
            TextBox_status2.Text = "匹配模板完成!请示教点位。"
            TextBox_status2.BackColor = Color.Lime
        End If
        If Vision_NG = "FIRST" Then
            TextBox_status2.Text = "匹配模板失败!第一个拍照点无法识别！"
            TextBox_status2.BackColor = Color.Red
        ElseIf Vision_NG = "SECOND" Then
            TextBox_status2.Text = "匹配模板失败!第二个拍照点无法识别！"
            TextBox_status2.BackColor = Color.Red
        End If

    End Sub

    Private Sub Button_Step1_Click(sender As Object, e As EventArgs) Handles Button_Step1.Click
        Dim result As Integer
        If Button_Step1.BackColor = Color.Lime Then
            Button_Step1.BackColor = Color.WhiteSmoke
            Button_Step1.UseVisualStyleBackColor = True
            StepNO = "STEP0"
            TextBox_status1.Text = ""
        Else
            Try
                Button_Step1.BackColor = Color.Lime
                StepNO = "STEP1"
                result = SendMsgToRobot("MANUAL")
                TextBox_status1.Text = "等待产品流入..."
                If result = 0 Then
                    Button_Step1.BackColor = Color.WhiteSmoke
                    Button_Step1.UseVisualStyleBackColor = True
                    StepNO = "STEP0"
                    TextBox_status1.Text = ""
                End If

            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        End If

    End Sub

End Class